import {
  BaseEntity,
  BeforeUpdate,
  Column,
  Entity,
  PrimaryGeneratedColumn,
} from "typeorm";

import { dbConfig } from "@/common/config";

@Entity()
export class User extends BaseEntity {
  @PrimaryGeneratedColumn()
  id: number;

  @Column({ type: "varchar", name: "account", length: 20, nullable: true })
  account: string;

  @Column({ type: "varchar", name: "password", length: 20, nullable: true })
  pwd: string;

  @Column({
    type: "varchar",
    name: "openid",
    length: 255,
    nullable: true,
    comment: "用户在微信开放平台上的唯一标识符（OpenID），用于标识微信用户",
  })
  openid: string;

  @Column({
    type: "varchar",
    name: "unionid",
    length: 255,
    nullable: true,
    comment:
      "可通过unionid来识别同一用户在这些有共同主体的应用与公众号之间的帐号",
  })
  unionid: string;

  @Column({
    type: "varchar",
    name: "nickname",
    length: 255,
    nullable: true,
    comment: "用户的昵称（微信昵称）",
  })
  nickname: string;

  @Column({
    type: "varchar",
    name: "avatar_url",
    length: 255,
    nullable: true,
    comment: "用户的头像URL",
  })
  avatar_url: string;

  @Column({
    type: "int",
    name: "gender",
    nullable: true,
    comment: "1男，2女，0未知",
  })
  gender: number;

  @Column({
    type: "varchar",
    name: "phone",
    length: 20,
    nullable: true,
    comment: "手机号码（需要用户授权获取)",
  })
  phone: string;

  @Column({
    type: "varchar",
    name: "email",
    length: 255,
    nullable: true,
    comment: "电子邮箱（需要用户授权获取或手动输入）",
  })
  email: string;

  @Column({
    type: "tinyint",
    name: "status",
    nullable: true,
    comment: "1活跃，0禁用",
  })
  status: number;

  @Column({
    type: "tinyint",
    name: "is_admin",
    nullable: true,
    comment: "1 管理员，0普通用户",
  })
  is_admin: number;

  @Column({
    type: "datetime",
    name: "last_login_time",
    nullable: true,
    comment: "1 管理员，0普通用户",
  })
  last_login_time: Date;

  @Column({
    type: dbConfig.systemDB === "sqlite" ? "datetime" : "timestamp",
    default: () => "CURRENT_TIMESTAMP",
  })
  created: Date;

  @Column({
    type: dbConfig.systemDB === "sqlite" ? "datetime" : "timestamp",
    default: () => "CURRENT_TIMESTAMP",
  })
  updated: Date;

  @BeforeUpdate()
  updateTimestamp() {
    this.updated = new Date();
  }
}
